<template>
  <div class="customerManage">
    <view-header :title="'设施维修'"></view-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="6">
            <el-form-item label="宿舍楼名称">
              <el-input type="text" v-model="queryForm.buildingName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="宿舍号">
              <el-input type="text" v-model="queryForm.dormitoryNumber"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="维修情况">
              <el-select style="width: 100%;" v-model="queryForm.maintainFlag" placeholder="是否完成维修：" clearable>
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上报日期">
              <el-date-picker
                style="width: 100%;"
                v-model="queryForm.submitDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="24">
          <div class="btn_area">
            <el-button class="confirm" @click="btnClick">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confirm" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog title="填写报修项" :visible.sync="visible" width="60%" @close="close">
      <el-row>
        <el-form :form="maintenanceForm" label-width="1rem">
          <el-col :span="22">
            <el-form-item label="维修项：">
              <el-input type="textarea" v-model="maintenanceForm.maintainItem" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="上传图片：">
              
              <el-image
                v-if="maintenanceForm.maintainPicture" 
                style="width: 50%; height: 150px"
                :src="maintenanceForm.maintainPicture" 
                :preview-src-list="[maintenanceForm.maintainPicture]"
                fit="scale-down">
              </el-image>
              <el-button @click="openPhoto" v-else  type="primary">点击上传</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confirm" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="报修详情" :visible.sync="visible2" width="60%" @close="close">
      <el-descriptions direction="vertical" :column="3" border>
        <el-descriptions-item label="所属宿舍楼" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.buildingName }}</el-descriptions-item>
        <el-descriptions-item label="宿舍管理员" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.buildingName }}</el-descriptions-item>
        <el-descriptions-item label="宿舍号" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.dormitoryNumber }}</el-descriptions-item>
        <el-descriptions-item label="上报人" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.submitUser }}</el-descriptions-item>
        <el-descriptions-item label="上报日期" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.submitDate }}</el-descriptions-item>
        <el-descriptions-item label="维修人员" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.userName ? maintenanceForm.userName : '-'}}</el-descriptions-item>
        <el-descriptions-item label="联系方式" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.staffPhone ? maintenanceForm.staffPhone : '-'}}</el-descriptions-item>
        <el-descriptions-item label="维修情况" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.maintainFlag == '0' ? '未维修' : '已维修' }}</el-descriptions-item>
        <el-descriptions-item label="维修日期" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.maintainDate ? maintenanceForm.maintainDate : '-'}}</el-descriptions-item>

        <el-descriptions-item label="维修项" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">{{ maintenanceForm.maintainItem ? maintenanceForm.maintainItem : '-'}}</el-descriptions-item>
        <el-descriptions-item label="图片说明" :contentStyle="{'text-align': 'center'}" :labelStyle="{'text-align': 'center'}">
          <span v-if="!maintenanceForm.maintainPicture">暂无图片说明</span>
          <el-image v-else :src="maintenanceForm.maintainPicture" alt="" style="width: 0.6rem;height:0.6rem;"
            :preview-src-list="[maintenanceForm.maintainPicture]">
          </el-image>
        </el-descriptions-item>
      </el-descriptions>
      <div v-if="!maintenanceForm.staffId && (user.permission == '01' || user.permission == '02')" style="margin-top: 0.2rem;display: flex;align-items: center;justify-content: center;">
        <div>分配维修人员：</div>
        <el-select v-model="staffId" placeholder="请选择维修人员">
          <el-option v-for="item in saffList" :key="item.uuid" :value="item.uuid" :label="item.userName"></el-option>
        </el-select>
      </div>
      <div v-if="maintenanceForm.maintainFlag == '0' && user.permission == '03'" style="margin-top: 0.2rem;display: flex;align-items: center;justify-content: center;">
        <div>选择维修日期：</div>
        <el-date-picker
          v-model="date"
          type="date"
          placeholder="选择维修日期："
          value-format="yyyy-MM-dd"
          clearable>
        </el-date-picker>
        <div style="margin-left: 0.2rem;">是否完成维修：</div>
        <el-select v-model="flag" placeholder="是否完成维修：">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible2 = false">关 闭</el-button>
        <el-button  v-if="!maintenanceForm.staffId && (user.permission == '01' || user.permission == '02')" class="confirm" @click="fenpei">确 定</el-button>
        <el-button  v-if="maintenanceForm.maintainFlag == '0' && user.permission == '03'" class="confirm" @click="wancheng">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="上传图片" :visible.sync="photoVisible" width="35%" :before-close="close1" :modal="false" :append-to-body="false" class="xiugaimima dialog_box">
      <el-upload ref="upload" class="avatar-uploader" action="" :show-file-list="false" :auto-upload="false"
        :on-change="updatachange" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button class="confirm" @click="uploadImg">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import viewHeader from '@/components/viewHeader.vue'
export default {
  name: 'customerManagement',
  components: {
    tableList,
    viewHeader
  },
  data() {
    return {
      date: '',
      flag: '',
      staffId: '',
      visible2: false,
      maintenanceForm: {},
      queryForm: {
        "buildingName": "",
        "dormitoryNumber": "",
        "maintainFlag": "",
        "submitDate": ""
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'buildingName',
          label: '宿舍楼名称',
          align: 'center'
        },
        {
          prop: 'dormitoryNumber',
          label: '宿舍号',
          align: 'center'
        },
        {
          prop: 'submitDate',
          label: '上报日期',
          align: 'center'
        },
        {
          prop: 'submitUser',
          label: '上报人',
          align: 'center'
        },
        {
          prop: 'userName',
          label: '维修人',
          align: 'center'
        },
        {
          prop: 'staffPhone',
          label: '联系方式',
          align: 'center'
        },
        {
          prop: 'maintainDate',
          label: '维修日期',
          align: 'center'
        },
        {
          prop: 'maintainFlagName',
          label: '维修情况',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'primary',desc:'查看详情'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      user: {},
      visible: false,
      code: '',
      saffList: [],
      photoVisible: false,
      imageUrl: "",
      imageHead: "",
    }
  },
  methods: {
    close1() {
      this.photoVisible = false;
    },
    uploadImg() {
      let url = this.$api.common.photo;
      let fileFormData = new FormData();
      fileFormData.append("file", this.imageHead);
      this.$http(url, fileFormData, "", true).then((res) => {
        if (res.data.code === 200) {
          this.maintenanceForm.maintainPicture = res.data.data
          this.photoVisible = false;
        } else {
          this.$message({
            message: res.data.message,
            type: "error",
            duration: 2000,
          });
        }
      });
    },
    updatachange(res) {
      this.imageUrl = URL.createObjectURL(res.raw);
      this.imageHead = res.raw;
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/jpg" ||
        file.type === "image/png";
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      return isJPG;
    },
    openPhoto() {
      if (this.$refs.upload) {
        this.$refs.upload.clearFiles();
        this.imageUrl = "";
        this.imageHead = "";
      }
      this.photoVisible = true;
    },
    onHandle(data) {
      let {row} = data;
      this.$http(this.$api.maintenance.info,this.reqData(row.uuid)).then(res => {
        if (res.data.code === 200) {
          this.maintenanceForm = res.data.data;
          console.log(this.maintenanceForm);
          this.visible2 = true
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    btnClick() {
      console.log(111);
      this.queryMaintenanceList();
    },
    add() {
      this.visible = true;
      this.code = '1';
    },
    wancheng() {
      this.maintenanceForm.maintainDate = this.date;
      this.maintenanceForm.maintainFlag = this.flag;
      console.log(this.maintenanceForm);
      this.update();
    },
    fenpei() {
      this.maintenanceForm.staffId = this.staffId;
      console.log(this.maintenanceForm);
      this.update();
    },
    update() {
      let url = this.$api.maintenance.update;
      this.$http(url,this.reqData(this.maintenanceForm)).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message)
          this.visible2 = false;
          this.queryMaintenanceList();
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    reset() {
      this.queryForm = {
        "buildingName": "",
        "dormitoryNumber": "",
        "maintainFlag": "",
        "submitDate": ""
      }
    },
    close() {
      this.maintenanceForm = {};
    },
    confirm() {
      let url = this.$api.maintenance.create;
      let obj = {
        userId: this.user.uuid,
        dormitoryId: this.user.dormitoryId,
        maintainItem: this.maintenanceForm.maintainItem,
        maintainPicture: this.maintenanceForm.maintainPicture,
      }
      console.log(url,obj);
      this.$http(url,this.reqData(obj)).then(res => {
        if (res.data.code == 200) {
          this.$message.success('已上报')
          this.visible = false;
          this.queryMaintenanceList();
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    queryMaintenanceList() {
      this.queryForm.permission = this.user.permission;
      this.queryForm.adminId = this.user.uuid;
      this.$http(this.$api.maintenance.list,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data;
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1
            item.maintainFlagName = item.maintainFlag == '0' ? '未维修' : '已维修'
          })
          this.total = this.list.length;
          console.log(data);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
    },
    querySaffList() {
      this.$http(this.$api.user.list,this.reqData({queryPermission: '03'},1,9999)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data;
          this.saffList = data.list
          console.log(this.saffList);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
  },
  created() {
    this.user = JSON.parse(sessionStorage.getItem('user'));
  },
  mounted() {
    this.queryMaintenanceList();
    this.querySaffList();
  }
}
</script>
<style lang="less" scoped>
.customerManage {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
  /deep/ .xiugaimima {
    .el-dialog {
      .el-dialog__body {
        padding: 0.1rem 0 0.1rem 0;
        /*padding-left: 0 !important;*/
        display: flex;
        justify-content: center;

        .el-form {
          width: 80%;

          .el-form-item {
            margin: 0 !important;
            margin-top: 0.25rem !important;
            .el-form-item__label {
              font-size: 0.18rem !important;
            }
          }
        }
      }

      .el-dialog__footer {
        text-align: center;
      }
    }
  }

  .xiugaimima {
    /deep/ .avatar-uploader .el-upload {
      border: 1px solid #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }

    /deep/ .avatar-uploader .el-upload:hover {
      border-color: #9c88ff;
    }

    /deep/ .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    /deep/ .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}
</style>